<?php
/**
 * 手机端 - 作品
 * User: eecjimmy
 * Date: 2015-06-17
 */

namespace Wap\Controller;

use Think\Page;

class WorksController extends WapController {
    /**
     * @var \Think\Model
     */
    private $m;

    function __construct() {
        parent::__construct();
        $this->m = M('works');
    }

    // 作品列表
    function index() {
        $limit = 5; // 每页条数
        $where = array();
        $order = array('sort asc',);
        $field = array(
            'faxiu_works.id',
            'faxiu_works.wname',
            'faxiu_works.explain',
            'faxiu_works.content',
            'faxiu_picture.path as pic_path'
        );
        $join = array(
            'LEFT JOIN faxiu_picture ON faxiu_picture.id=pic',
        );

        // 分页
        $count = $this->m->where($where)->count();
        $page = new Page($count, $limit, $_REQUEST);
        if ($count > $limit) {
            $page->setConfig('theme', '%DOWN_PAGE%');
            $page->setConfig('next', '加载更多...');
        }
        $pageNav = $page->show();
        $offset = $page->firstRow;

        $list = $this->m->where($where)->join($join)->order($order)->limit($offset, $limit)->field($field)->select();
        $this->assign('list', $list);
        if (!IS_AJAX) {
            $this->assign('pageNav', $pageNav);
            $this->display();
        } else {
            $this->ajaxReturn(array('html' => $this->fetch('list'), 'pageNav' => $pageNav));
        }
    }

    // 作品详情
    function detail($id) {
        $where = array(
            'faxiu_works.id' => (int)$id,
        );
        $field = array(
//            'faxiu_picture.path pic_path',
            'faxiu_works.*',
        );
        $join = array(
//            'LEFT JOIN faxiu_picture ON faxiu_picture.id=faxiu_works.pic',
        );
        $work = $this->m->where($where)->join($join)->field($field)->find();
        $work === null && E('作品不存在');
        $pics = explode(',',$work['pic']);
        unset($work['pic']);
        foreach($pics as $pic_id){
            $work['pic'][] = get_cover($pic_id,'path');
        }
        $work['is_collect'] = $this->_is_collected($work['did'], 4);        // 判断该当前微信号是否收藏该作品
        $work['collect_total'] = M('collect')->where(array('tid' => $work['id'], 'type' => 4))->count(); // 收藏量
        $work['designer'] = $this->get_designer($work['did']); // 该作品的设计师
        $this->assign('new_works',R('Designer/_get_works_by_designer',array('designer_id'=>$work['did'],null,"id"=>(int)$id))); // 该作者的最新作品
        $this->assign('work', $work);
        $this->display();
    }

    // 收藏作品
    function collect($wid) {
        // 判断是否收藏过该作品
        if ($this->_is_collected($wid, 4)) {
            $where['mid'] = $this->member['id'];
            $where['tid'] = $wid;
            if (M('collect')->delete($where)) {
                $this->ajaxReturn(array('status' => true, 'action' => 'remove', 'info' => 'success'));
            } else {
                $this->ajaxReturn(array('status' => false, 'info' => '取消收藏失败'));
            }
        } else {
            $data['type'] = 4;
            $data['mid'] = $this->member['id'];
            $data['tid'] = $wid;
            $data['add_time'] = NOW_TIME;
            if (M('collect')->add($data)) {
                $this->ajaxReturn(array('status' => true, 'action' => 'add', 'info' => 'success'));
            } else {
                $this->ajaxReturn(array('status' => false, 'info' => '收藏失败'));
            }
        }
    }

    /**
     * 获取设计师
     * @param $designer_id
     * @return array
     */
    function get_designer($designer_id){
        $where['faxiu_designer.id'] = $designer_id; // 条件处理
        $join = array(
            'faxiu_collect on faxiu_collect.tid=faxiu_designer.id'
        );
        $field = array(
            'faxiu_designer.*',
            'count(faxiu_collect.id) as collect_num'
        );
        $info = M('designer')->where($where)->join($join)->field($field)->find();
        // 解析图片
        $pics = explode(',',$info['pic']);
        unset($info['pic']);
        foreach($pics as $pic_id){
            $info['pic'][] = get_cover($pic_id,'path');
        }
        return $info;
    }
}
